﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// params动态参数缓存
    /// </summary>
    public static class ParamsHelper
    {
        private static readonly Dictionary<int, Queue<object[]>> objpool = new Dictionary<int, Queue<object[]>>();

        /// <summary>
        /// 获取指定大小的缓存
        /// </summary>
        /// <param name="num">期望的缓存大小</param>
        /// <returns></returns>
        public static object[] GetObj(int num)
        {
            Queue<object[]> queue;
            object[] obj;
            if (!objpool.TryGetValue(num, out queue))
            {
                queue = new Queue<object[]>();
                objpool.Add(num, queue);
            }
            if (queue.Count > 0)
            {
                obj = queue.Dequeue();
            }
            else
            {
                obj = new object[num];
            }
            return obj;
        }

        /// <summary>
        /// 归还使用的Params参数缓存
        /// </summary>
        /// <param name="obj">缓存</param>
        public static void ReturnObj(object[] obj)
        {
            Queue<object[]> queue;
            if (!objpool.TryGetValue(obj.Length, out queue))
            {
                queue = new Queue<object[]>();
                objpool.Add(obj.Length, queue);
            }
            queue.Enqueue(obj);
        }

    }
}
